// 通用功能
document.addEventListener('DOMContentLoaded', function() {
    // 语言选择器功能
    const languageSelector = document.querySelector('.language-selector');
    if (languageSelector) {
        languageSelector.addEventListener('click', function() {
            // 显示语言选择菜单
            alert('语言切换功能即将上线，敬请期待！');
        });
    }
    
    // 菜单按钮功能
    const menuBtn = document.querySelector('.menu-btn');
    if (menuBtn) {
        menuBtn.addEventListener('click', function() {
            // 显示菜单
            alert('菜单功能即将上线，敬请期待！');
        });
    }
    
    // 从本地存储加载设置
    loadSettings();
});

// 加载用户设置
function loadSettings() {
    // 应用界面语言
    const appLanguage = localStorage.getItem('preferredLanguage') || 'zh';
    
    // 应用其他设置
    const darkMode = localStorage.getItem('darkMode') === 'true';
    if (darkMode) {
        document.body.classList.add('dark-mode');
    }
}

// 翻译服务
window.TranslationService = {
    // 文本翻译
    translate: async function(text, sourceLang, targetLang) {
        try {
            const response = await fetch('/api/translate', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    text: text,
                    source_lang: sourceLang,
                    target_lang: targetLang
                }),
            });
            
            const data = await response.json();
            if (data.error) {
                throw new Error(data.error);
            }
            
            return data.translated_text;
        } catch (error) {
            console.error('翻译错误:', error);
            throw error;
        }
    },
    
    // 情境化翻译
    contextualTranslate: async function(text, context, sourceLang, targetLang) {
        try {
            const response = await fetch('/api/translate/contextual', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    text: text,
                    context: context,
                    source_lang: sourceLang,
                    target_lang: targetLang
                }),
            });
            
            const data = await response.json();
            if (data.error) {
                throw new Error(data.error);
            }
            
            return data.translations;
        } catch (error) {
            console.error('情境化翻译错误:', error);
            throw error;
        }
    },
    
    // 获取文化信息
    getCultureInfo: async function(query, location) {
        try {
            const response = await fetch('/api/culture', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    query: query,
                    location: location || ''
                }),
            });
            
            const data = await response.json();
            if (data.error) {
                throw new Error(data.error);
            }
            
            return data.culture_info;
        } catch (error) {
            console.error('获取文化信息错误:', error);
            throw error;
        }
    }
};

// 离线内容管理
window.OfflineContentManager = {
    // 获取可用目的地
    getAvailableDestinations: async function() {
        try {
            const response = await fetch('/api/available_destinations');
            const data = await response.json();
            return data.destinations;
        } catch (error) {
            console.error('获取目的地信息失败:', error);
            throw error;
        }
    },
    
    // 下载内容包
    downloadPackage: async function(destination, packageType) {
        try {
            const response = await fetch('/api/download_package', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    destination: destination,
                    package_type: packageType
                }),
            });
            
            const data = await response.json();
            if (!response.ok) {
                throw new Error(data.error || '下载失败');
            }
            
            return data;
        } catch (error) {
            console.error('下载错误:', error);
            throw error;
        }
    },
    
    // 检查下载状态
    checkDownloadStatus: async function(downloadId) {
        try {
            const response = await fetch(`/api/download_status/${downloadId}`);
            const data = await response.json();
            
            if (!response.ok) {
                throw new Error(data.error || '获取状态失败');
            }
            
            return data;
        } catch (error) {
            console.error('获取下载状态错误:', error);
            throw error;
        }
    },
    
    // 获取存储信息
    getStorageInfo: async function() {
        try {
            const response = await fetch('/api/storage/info');
            const data = await response.json();
            return data;
        } catch (error) {
            console.error('获取存储信息失败:', error);
            throw error;
        }
    },
    
    // 删除内容包
    deletePackage: async function(downloadId) {
        try {
            const response = await fetch(`/api/storage/delete/${downloadId}`, {
                method: 'DELETE'
            });
            
            const data = await response.json();
            if (!response.ok) {
                throw new Error(data.error || '删除失败');
            }
            
            return data;
        } catch (error) {
            console.error('删除内容包失败:', error);
            throw error;
        }
    }
};

// 文本朗读功能
function speakText(text, lang) {
    if ('speechSynthesis' in window) {
        const utterance = new SpeechSynthesisUtterance(text);
        utterance.lang = lang === 'en' ? 'en-US' : lang;
        window.speechSynthesis.speak(utterance);
    } else {
        console.log('浏览器不支持语音合成');
    }
}